
/*
 *	윈도우용 소켓(WinSock) 구현
 */

#ifdef	_WIN32

#include <mh/network/socket_win32.hpp>

namespace Mh
{
	namespace Network
	{
		::HMODULE	SocketWin32::s_module;
		::WSADATA	SocketWin32::s_wsa_data;
		
		SocketWin32::SocketWin32()
			:	m_handle( INVALID_SOCKET )
		{}

		SocketWin32::~SocketWin32()
		{}

		int SocketWin32::create( int af, int type, int protocol )
		{
		//	m_handle = ::socket( af, type, protocol );
		//	return ( INVALID_SOCKET == m_handle ? ::WSAGetLastError() : 0 );
			return 0;
		}

		bool SocketWin32::bind( const Mh::Network::SockAddr *addr, size_t addr_size )
		{
			return 0;
		}

		bool SocketWin32::connect(const Mh::Network::SockAddr *addr, size_t *addr_size)
		{
			return false;
		}

		int SocketWin32::startup( WORD version )
		{
			// 라이브러리 로드
			s_module = ::LoadLibrary( TEXT("WS2_32.dll") );
			if( !s_module )
				return -1;
			
			typedef int (PASCAL FAR *WSStartupFunc)( WORD, ::LPWSADATA );
			WSStartupFunc func = (WSStartupFunc)::GetProcAddress( s_module, "WSAStartup" );
			if( !func )
				return -1;			

			return (*func)( version, &s_wsa_data );
		}

		int SocketWin32::cleanup()
		{
			int result = 0;
			if( s_module )
			{
				typedef int (PASCAL FAR *WSACleanupFunc)();
				WSACleanupFunc func = (WSACleanupFunc)::GetProcAddress( s_module, "WSACleanup" );
				if( !func )
				{
					result = -1;
				}
				else
				{
					result = (*func)();
					if( 0 == result )
						ZeroMemory( &s_wsa_data, sizeof(WSADATA) );
				}

				FreeLibrary( s_module );
				s_module = NULL;
			}

			return result;
		}

	}	// namespace Mh::Network
}		// namespace Mh

#endif	// ifdef _WIN32